define(['jquery', 'bootstrap', 'backend', 'table', 'form','upload'], function ($, undefined, Backend, Table, Form ,Upload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kzvote/vote/index' + location.search,
                    add_url: 'kzvote/vote/add',
                    edit_url: 'kzvote/vote/edit',
                    del_url: 'kzvote/vote/del',
                    multi_url: 'kzvote/vote/multi',
                    import_url: 'kzvote/vote/import',
                    table: 'kzvote_vote',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                showExport: false,//隐藏导出
                showToggle: false,//隐藏浏览模式
                showColumns: false,//隐藏显示隐藏模式
                searchFormVisible: true,//默认显示搜索表单
                search: false,//默认显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'mold', title: __('Mold'), searchList: {"1":__('Mold 1'),"2":__('Mold 2'),"3":__('Mold 3'),"4":__('Mold 4')}, formatter: Table.api.formatter.normal},
                        // {field: 'user', title: __('归属'), operate: false, formatter: Controller.api.formatter.user},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'start_time', title: __('Start_time'), operate: false, formatter: Controller.api.formatter.time},
                        {field: 'poster', title: __('Poster'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'person_poster', title: __('Person Poster'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'count', title: __('统计'), operate: false, formatter: Controller.api.formatter.amount},
                        {field: 'er_code', title: __('Er_code'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        { field: 'check', title: __('Check'), searchList: { "0": __('Check 0'), "1": __('Check 1'), "2": __('Check 2') }, formatter: Table.api.formatter.status },
                        {field: 'status', title: __('Status'), searchList: {"10":__('Status 10'),"20":__('Status 20'),"30":__('Status 30')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    dropdown: "更多",
                                    name: 'person',
                                    text: __('作品管理'),
                                    title: __('作品管理'),
                                    classname: 'btn btn-xs btn-dialog',
                                    url: 'kzvote/person/index?vote_id={ids}',
                                },
                                {
                                    dropdown: "更多",
                                    name: 'group',
                                    text: __('分组管理'),
                                    title: __('分组管理'),
                                    classname: 'btn btn-xs btn-dialog',
                                    url: 'kzvote/group/index?vote_id={ids}',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.group_status == '1' && row.mold < 3)
                                            return true;
                                    }
                                },
                                {
                                    dropdown: "更多",
                                    name: 'review',
                                    text: __('弹幕管理'),
                                    title: __('弹幕管理'),
                                    classname: 'btn btn-xs btn-dialog',
                                    url: 'kzvote/review/index?vote_id={ids}',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.review_status == '1' && row.mold < 3)
                                            return true;
                                    }
                                },
                                {
                                    dropdown: "更多",
                                    name: 'copy',
                                    text: __('复制活动'),
                                    title: __('复制活动'),
                                    classname: 'btn btn-xs btn-ajax',
                                    url: 'kzvote/vote/copy',
                                    confirm: '是否确认复制活动？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        Layer.alert('复制活动成功');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    dropdown: "更多",
                                    name: 'qrcode',
                                    text: __('小程序码'),
                                    title: __('重新生成小程序码'),
                                    classname: 'btn btn-xs btn-ajax',
                                    url: 'kzvote/vote/erCode',
                                    confirm: '是否确认重新生成小程序码？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        Layer.alert(ret.msg);
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.operate, operate: false
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        case: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kzvote/vote/case' + location.search,
                    multi_url: 'kzvote/vote/multi',
                    table: 'kzvote_vote',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                showExport: false,//隐藏导出
                showToggle: false,//隐藏浏览模式
                showColumns: false,//隐藏显示隐藏模式
                searchFormVisible: true,//默认显示搜索表单
                search: false,//默认显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'mold', title: __('Mold'), searchList: {"1":__('Mold 1'),"2":__('Mold 2'),"3":__('Mold 3'),"4":__('Mold 4')}, formatter: Table.api.formatter.normal},
                        // {field: 'user', title: __('归属'), operate: false, formatter: Controller.api.formatter.user},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'start_time', title: __('Start_time'), operate: false, formatter: Controller.api.formatter.time},
                        {field: 'count', title: __('统计'), operate: false, formatter: Controller.api.formatter.amount},
                        {field: 'er_code', title: __('Er_code'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        { field: 'check', title: __('Check'), searchList: { "0": __('Check 0'), "1": __('Check 1'), "2": __('Check 2') }, formatter: Table.api.formatter.status },
                        {field: 'status', title: __('Status'), searchList: {"10":__('Status 10'),"20":__('Status 20'),"30":__('Status 30')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                // $("#c-temp_name").data("eSelect", function(res){
                //     //后续操作
                //     $("#c-name").val(res.title)
                //     getCanvasBase64(res.head)
                //         .then(function (base64) {
                //             var arr = base64.split(','),
                //                 mime = arr[0].match(/:(.*?);/)[1],
                //                 bstr = atob(arr[1]),
                //                 n = bstr.length,
                //                 u8arr = new Uint8Array(n);
                //             while (n--) {
                //                 u8arr[n] = bstr.charCodeAt(n);
                //             }
                //             var file = new File([u8arr], Math.floor(Math.random()*(100-50))+50+'.'+mime.slice(6), {type: mime});
                //             Upload.api.send(file, function (data) {
                //                 $("#c-image").val(data.url).trigger("change")
                //                 // $("#c-share_image").val(data.url).trigger("change")
                //             });
                //         }, function (err) {
                //             console.log(err);
                //         });
                // });
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                time: function(val,row){
                    return row.start_time_text + '<br/>' + row.end_time_text
                },
                user: function(val,row){
                    if (row.user_id > 0){
                        return '<span style="color: forestgreen">'+row.user.nickname+'</span>';
                    }else{
                        return '后台创建';
                    }
                },
                amount: function(val,row){
                    return '投票数：'+row.count+'<br/>浏览量：'+row.hit+'<br/>作品人数：'+row.person_count
                }
            }
        }
    };
    return Controller;
});
// function getCanvasBase64(img) {
//     var image = new Image();
//     image.crossOrigin = '';
//     image.src = img;
//     var deferred = $.Deferred();
//     if (img) {
//         image.onload = function () {
//             var canvas = document.createElement("canvas");
//             canvas.width = image.width;
//             canvas.height = image.height;
//             var ctx = canvas.getContext("2d");
//             ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
//             deferred.resolve(canvas.toDataURL());//将base64传给done上传处理
//         }
//         return deferred.promise();//问题要让onload完成后再return sessionStorage['imgTest']
//     }
// }